package com.hopu.oa.controller.management;

import com.hopu.oa.entity.management.AoaAttendsList;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.management.AttendanceManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: AttendanceManagementServiceImpl
 * @Description:
 * @Date: 2021/11/19 21:14
 */

@Controller
public class AttendanceManagementController {
    @Autowired
    private AttendanceManagementService attendanceManagementService;

    /*
      @Description : 外层
      @Date   :  2021/11/19
      @Param  : [page, size, session, model, baseKey]
      @return : java.lang.String
     */
    @RequestMapping("attendceatt")
    public String attendceatt(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "8") int size,
            HttpSession session,
            Model model,
            String baseKey,
            @RequestParam(value = "typeSelect", defaultValue = "0") int typeId

    ) {

        //获取当前用户
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");


        //获取总数据
        int totalElements = attendanceManagementService.
                queryAttendanceByDeptAndRole(aoaUser.getRoleId(), aoaUser.getDeptId(), (page - 1) * size, 0, baseKey, typeId).size();

        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;

        //条件查询所有考勤
        List<AoaAttendsList> attends = attendanceManagementService.
                queryAttendanceByDeptAndRole(aoaUser.getRoleId(), aoaUser.getDeptId(), (page - 1) * size, size, baseKey, typeId);

        //查询所有状态
        List<AoaStatusList> status = attendanceManagementService.
                queryAllStatus(0);

        //查询所有类型
        List<AoaTypeList> types = attendanceManagementService.
                queryAllType();


        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);


        model.addAttribute("alist", attends);
        model.addAttribute("statuslist", status);
        model.addAttribute("typelist", types);
        model.addAttribute("page", map);
        model.addAttribute("url", "attendcetable");
        model.addAttribute("baseKey", baseKey);


        return "attendce/attendceview";
    }


    /*
      @Description : 内层
      @Date   :  2021/11/19
      @Param  : [page, size, session, model, baseKey]
      @return : java.lang.String
     */
    @RequestMapping("attendcetable")
    public String attendceatt2(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "8") int size,
            HttpSession session,
            Model model,
            String baseKey,
            @RequestParam(value = "typeSelect", defaultValue = "0") int typeId
    ) {

        //获取当前用户
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");


        //获取总数据
        int totalElements = attendanceManagementService.
                queryAttendanceByDeptAndRole(aoaUser.getRoleId(), aoaUser.getDeptId(), (page - 1) * size, 0, baseKey, typeId).size();

        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;

        //条件查询所有考勤
        List<AoaAttendsList> attends = attendanceManagementService.
                queryAttendanceByDeptAndRole(aoaUser.getRoleId(), aoaUser.getDeptId(), (page - 1) * size, size, baseKey, typeId);

        //查询所有状态
        List<AoaStatusList> status = attendanceManagementService.
                queryAllStatus(0);

        //查询所有类型
        List<AoaTypeList> types = attendanceManagementService.
                queryAllType();

        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);

        model.addAttribute("alist", attends);
        model.addAttribute("statuslist", status);
        model.addAttribute("typelist", types);
        model.addAttribute("page", map);
        model.addAttribute("url", "attendcetable");
        model.addAttribute("baseKey", baseKey);

        return "attendce/attendcetable";
    }


    /*
      @Description : 修改考勤
      @Date   :  2021/11/19
      @Param  : []
      @return : java.lang.String
     */
    @RequestMapping("/attendceedit")
    public String addendceedit(String aid, String tid, Model model, String thisPage,
                               @RequestParam(value = "baseKey", defaultValue = "") String baseKey) {
        System.out.println("thisPage=" + thisPage);
        System.out.println("aid = " + aid);
        System.out.println("tid = " + tid);

        //查询所有类型
        List<AoaTypeList> types = attendanceManagementService.
                queryAllType();

        // 查询一个考勤
        AoaAttendsList attends = attendanceManagementService.queryAttendanceById(aid);


        //查询所有状态
        List<AoaStatusList> status = attendanceManagementService.
                queryAllStatus(Integer.parseInt(tid));


        model.addAttribute("typelist", types);
        model.addAttribute("statuslist", status);
        model.addAttribute("attends", attends);
        model.addAttribute("thisPage", thisPage);
        model.addAttribute("baseKey", baseKey);

        return "attendce/attendceedit";

    }

    /*
      @Description : 修改考勤
      @Date   :  2021/11/20
      @Param  : [aid, sid]
      @return : java.lang.Boolean
     */
    @RequestMapping("updateAttendanceType")
    @ResponseBody
    public Boolean updateAttendanceType(String aid, String sid) {
        return attendanceManagementService.updateAttendanceStatusById(aid, sid);
    }


    @RequestMapping("deleteAttendanceType")
    public String deleteAttendanceType(String aid, String page) {
        System.out.println("=========");
        boolean b = attendanceManagementService.deleteAttendanceById(Integer.parseInt(aid));
        System.out.println(b);
        return "forward:attendceatt?page=" + page;
    }


}
